﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities.Excel.Xml;
using NPOI.HSSF.UserModel;
using System.Reflection;
using NPOI.SS.UserModel;

namespace Common.Utilities.Excel.Export
{
    public class Font : CellStyleBase
    {
        #region Properties

        /// <summary>
        /// 字体实例, 避免重复创建字体
        /// </summary>
        protected IFont HSSFFont { get; set; }

        public CellStyle CellStyle { get; set; }

        #endregion

        #region ctor

        public Font(XmlNode fontNode, CellStyle cellStyle)
            : base(fontNode)
        {
            CellStyle = cellStyle;
        }

        #endregion

        #region Methods

        /// <summary>
        /// 供外部调用, 把当前的字体样式设置到单元格中
        /// </summary>
        /// <param name="workbook"></param>
        public void SetCellFont(IWorkbook workbook)
        {
            SetCellFont(CellStyle.Style, workbook);
        }

        /// <summary>
        /// 供外部调用, 把当前的字体样式设置到单元格中
        /// </summary>
        /// <param name="workbook"></param>
        public void SetCellFont(ICellStyle cellStyle, IWorkbook workbook)
        {
            // 如果第一次执行此方法, 则HSSFFont为null
            // 之后执行的话, 就可以直接取这个Font了
            if (HSSFFont == null)
            {
                HSSFFont = workbook.CreateFont();
                Type fontType = HSSFFont.GetType();
                // 反射形式赋值
                for (int iLoop = 0; iLoop < Properties.Count; iLoop++)
                {
                    PropertyInfo property = fontType.GetProperty(Properties[iLoop]);
                    object value = Arguments[iLoop].GetValue();
                    property.SetValue(HSSFFont, value, null);
                }
            }
            cellStyle.SetFont(HSSFFont);
        }

        #endregion
    }

    
}
